"""
Definition file, contains the structures used in parsing .csp files into a 1-level CSP Network.

Copyright (c) 2008 Rune M. Friborg, runef@diku.dk.
See LICENSE.txt for licensing details (GPLv2).
"""

# Process / Component definitions
class ProcessDef ():
   def __init__(self, name, codeObj):
      self.name = name                  # str
      self.codeObj = codeObj            # obj
      self.finalChannelEndList = None   # list of str
      
class CodeDef ():
   def __init__(self, funcName, funcObj, channelEndObjList):
      self.funcName = funcName                   # str
      self.funcObj = funcObj                     # function
      self.channelEndObjList = channelEndObjList # obj


class ChannelEndDef ():
   def __init__(self, direction, name):
      self.direction = direction  # 'in' | 'out'
      self.name = name            # str


# Channel definitions step 1
class CPointDef ():
   def __init__(self, name, plugin_attr = {}):
      self.name = name               # str

      # Used for plugins
      self.plugin_attr = plugin_attr

      # Tag as not removed
      self.REMOVE = False

class ChannelLineDef ():
   def __init__(self, name, inTarget, outTarget, plugin_attr = {}):
      self.name = name           # str
      self.inTarget = inTarget   # str
      self.outTarget = outTarget # str

      self.cpoint_conn = False

      # Used for plugins
      self.plugin_attr = plugin_attr

      # Tag as not removed
      self.REMOVE = False

# Channel definitions step 2 ( Reduced )
class ConnectionDef ():
   def __init__(self, target, direction):
      self.target = target         # str
      self.direction = direction   # 'in' | 'out'

   def to_string(self):
      return self.target + ' ' + self.direction

class ReducedChannelDef ():
   def __init__(self, name, plugin_attr = {}):
      self.name = name            # str
      self.connectionObjList = [] # list of obj

      # Used for plugins
      self.plugin_attr = plugin_attr

      # Container for PyCSP channel instance
      self.instance = None

   def addAndLookupPaths(self, channelLookup, path = ''):
      for connection in self.connectionObjList:
         target = path + '.' + connection.target
         while (channelLookup.has_key(target)):
            target = channelLookup[target][0]
         connection.target = target
      # Fix name for debugging.
      self.name = path + '.' + self.name


   def to_string(self):
      s = 'ReducedChannel: ' + self.name + '\n'
      for c in self.connectionObjList:
         s += '\tConnection: ' + c.to_string() + '\n'
      return s

